import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:terminal_flutter_demo/pages/navigator_page.dart';

Map routes = {
  '/': (context, {arguments}) => NavigatorPage(
      title: arguments?['title'] ?? '主页', arguments: arguments ?? {}),
  '/news': (context, {arguments}) =>
      NavigatorPage(title: 'news', arguments: arguments ?? {'count': 0}),
  '/shop': (context, {arguments}) =>
      NavigatorPage(title: 'shop', arguments: arguments),
};

var onGenerateRoute = (RouteSettings settings) {
  var arguments = settings.arguments;
  print('settings: $settings');
  print('settings name: ${settings.name}');
  print('settings arguments: $arguments');
  final String? name = settings.name;
  final Function? pageContentBuilder = routes[name];
  // print('pageContentBuilder: $pageContentBuilder');
  if (pageContentBuilder != null) {
    if (arguments != null) {
      // iOS 风格
      final Route route = CupertinoPageRoute(
        builder: (context) => pageContentBuilder(
          context,
          arguments: arguments,
        ),
        settings: settings,
      );
      return route;
    } else {
      // Android 风格
      final Route route = MaterialPageRoute(
        builder: (context) => pageContentBuilder(
          context,
        ),
        settings: settings,
      );
      return route;
    }
  }
  return null;
};

class MyRouteObserver<R extends Route> extends RouteObserver<R> {
  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);
    print(
        'didPush route: ${route.settings.name},  previousRoute:${previousRoute?.settings.name}');
  }

  @override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);
    print(
        'didPop route: ${route.settings.name},  previousRoute:${previousRoute?.settings.name}');
  }

  @override
  void didReplace({Route? newRoute, Route? oldRoute}) {
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
    print('didReplace newRoute: $newRoute,oldRoute:$oldRoute');
  }

  @override
  void didRemove(Route route, Route? previousRoute) {
    super.didRemove(route, previousRoute);
    print('didRemove route: $route,previousRoute:$previousRoute');
  }

  @override
  void didStartUserGesture(Route route, Route? previousRoute) {
    super.didStartUserGesture(route, previousRoute);
    print('didStartUserGesture route: $route,previousRoute:$previousRoute');
  }

  @override
  void didStopUserGesture() {
    super.didStopUserGesture();
    print('didStopUserGesture');
  }
}
